package com.situ.hospital.filter;

import com.situ.hospital.pojo.Auth;
import com.situ.hospital.pojo.User;
import com.situ.hospital.service.IAuthService;
import com.situ.hospital.service.IUserService;
import com.situ.hospital.service.impl.AuthServiceImpl;
import com.situ.hospital.service.impl.UserServiceImpl;
import com.situ.hospital.util.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebFilter(filterName = "login", urlPatterns = "/*")
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("LoginFilter.init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("LoginFilter.doFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String uri = request.getRequestURI();
        System.out.println("uri: " + uri);
        String method = request.getParameter("method");
        System.out.println("method: " + method);
        if (uri.startsWith("/static")
                || uri.equals("/page/login")
                || uri.equals("/verifyCode")
                || uri.equals("/page")
                || (uri.equals("/user") && "login".equals(method))) {
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println("user: " + user);
        if(user == null) {
            Cookie cookie = CookieUtils.getCookie(request,"autoLogin");
            if (cookie != null) {
                String[] values = cookie.getValue().split("&");
                String name = values[0];
                String password = values[1];
                IUserService userService = new UserServiceImpl();
                user = userService.login(name, password);
                if (user != null) {
                    session.setAttribute("user",user);
                    IAuthService authService = new AuthServiceImpl();
                    List<Auth> authList = authService.selectByRole(user.getRole());
                    session.setAttribute("authList",authList);
                    filterChain.doFilter(servletRequest,servletResponse);
                    return;
                }
            }
            response.sendRedirect("/page/login");
            return;
        }

        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("LoginFilter.destroy");
    }
}
